package online.yysh.charging;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;


import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.bean.request.BindPhoneRequest;
import online.yysh.charging.bean.response.ErrorResponse;
import online.yysh.charging.livedata.BindPhoneManager;
import online.yysh.charging.utils.HttpUtil;
import online.yysh.charging.view.CountdownView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BindPhoneActivity extends BaseActivity {

    private CountdownView countdownView;
    private EditText et_phone, et_code;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        countdownView = findViewById(R.id.countdown_view);
        et_code = findViewById(R.id.et_code);
        et_phone = findViewById(R.id.et_phone);
        countdownView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = et_phone.getText().toString();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    showToast("手机号填写不正确，请重新输入");
                    return;
                }
                countdownView.start();
                getMessageCode(phone);
            }
        });
    }

    private void getMessageCode(String phone) {
        HttpUtil.getInstance().getMessageCode(phone, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                showToast("验证码获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                if (result.contains("ok")) {
                    showToast("验证码获取成功");
                } else {
                    showToast("验证码获取失败");
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    public void bind(View view) {
        final String phone = et_phone.getText().toString();
        String code = et_code.getText().toString();
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            return;
        }
        BindPhoneRequest bindPhoneRequest = new BindPhoneRequest(phone, code, openid);
        HttpUtil.getInstance().bindPhone(bindPhoneRequest, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                showToast("绑定失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                logE("result:" + result);
                if (result.contains("ok")) {
                    showToast("绑定成功");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            BindPhoneManager.getInstance().getLiveData().setValue(phone);
                            finish();
                        }
                    });
                } else {
                    ErrorResponse errorResponse = new Gson().fromJson(result,ErrorResponse.class);
                    showToast(errorResponse.getData());
                }
            }
        });
    }

}